package com.cata.mall.portal.componet;

import com.cata.mall.portal.domain.QueueEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @作者: Tao
 * @日期: 2025/10/16
 * @时间: 15:36
 * @描述: 取消订单消息的发送者 <pre>
 */
@Component
public class CancelOrderSender {
    private static final Logger LOGGER = LoggerFactory.getLogger(CancelOrderSender.class);
    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 发送延迟消息到队列，用于订单取消操作
     * @param orderId 订单ID，作为消息内容发送到队列
     * @param delayTimes 延迟时间（毫秒），消息将在指定时间后被消费
     */
    public void sendMessage(Long orderId, final long delayTimes) {
        //给延迟队列发送消息
        amqpTemplate.convertAndSend(QueueEnum.QUEUE_TTL_ORDER_CANCEL.getExchange(), QueueEnum.QUEUE_TTL_ORDER_CANCEL.getRouteKey(), orderId, message -> {
            //给消息设置延迟毫秒值
            message.getMessageProperties().setExpiration(String.valueOf(delayTimes));
            return message;
        });
        LOGGER.info("send orderId:{}", orderId);
    }

}
